//
//  zcViewController.m
//  何五路
//
//  Created by _ADY on 15/6/25.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "zcViewController.h"
#import "yzmViewController.h"

@interface zcViewController ()

@end
static LoadingView *loading = nil;
@implementation zcViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"手机快速注册";
        self.view.backgroundColor = bgColor;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    // Do any additional setup after loading the view.
    account = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, screenMySize.size.width-40, 40)];
    [account setFont:[UIFont systemFontOfSize:labelSize]];
    account.returnKeyType = UIReturnKeyNext;
    [account setClearButtonMode:UITextFieldViewModeWhileEditing];
    account.delegate = self;
    account.keyboardType = UIKeyboardTypeNumberPad;
    account.layer.cornerRadius=3.0f;
    account.layer.masksToBounds=YES;
    account.backgroundColor=[UIColor whiteColor];
    account.layer.borderColor=[[UIColor grayColor]CGColor];
    account.layer.borderWidth= 1.0f;
    [account setPlaceholder:@"请输入手机号"];
    [self.view  addSubview:account];
    
    
    password = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, 130, 40)];
    [password setFont:[UIFont systemFontOfSize:labelSize]];
    password.returnKeyType = UIReturnKeyGo;
    [password setClearButtonMode:UITextFieldViewModeWhileEditing];
    password.delegate = self;
    password.layer.cornerRadius=3.0f;
    password.layer.masksToBounds=YES;
    password.backgroundColor=[UIColor whiteColor];
    password.layer.borderColor=[[UIColor grayColor]CGColor];
    password.layer.borderWidth= 1.0f;
    password.text = @"admin@hewulu.com";
    [password setPlaceholder:@"推荐人"];
    [self.view  addSubview:password];
    

    yesBool = YES;
    UIImage *imageD = [UIImage imageNamed:@"xzPress"];
    inButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(10,200, 70, 30);
    [inButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    [inButton setTitle:@"同意" forState:UIControlStateNormal];
    [inButton setFrame:frame];
    [inButton setTitleEdgeInsets:UIEdgeInsetsMake(0, imageD.size.width, 0, 0)];
    [inButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [inButton setImage:imageD forState:UIControlStateNormal];
    [inButton addTarget:self action:@selector(aInBction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:inButton];
    
    UILabel *title1Label = [[UILabel alloc] initWithFrame:CGRectMake(80,200, screenMySize.size.width-90, 30)];
    title1Label.text = [NSString stringWithFormat:@"何五路用户注册协议"];
    title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize];
    title1Label.textColor = [UIColor blackColor];
    [self.view addSubview:title1Label];
    
    
    xButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [xButton setFrame:CGRectMake(15, 220+40, screenMySize.size.width-30, (screenMySize.size.width-30)*74/549)];
    [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    [xButton setTitle:@"下一步" forState:UIControlStateNormal];
    [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
    xButton.enabled = NO;
    
    [xButton addTarget:self action:@selector(xAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, xButton.frame.size.height+xButton.frame.origin.y+5, screenMySize.size.width-40, 25)];
    titleLabel.text = [NSString stringWithFormat:@"遇到问题？您可以联系客服。"];
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    titleLabel.textColor = [UIColor grayColor];
    [self.view addSubview:titleLabel];
    
}
-(void)mButon
{
    if (account.text.length!=0&&yesBool)
    {
        [xButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        xButton.enabled = YES;
        [xButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
    }
    else
    {
        [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
        xButton.enabled = NO;
        [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    }
}
-(void)aInBction
{
    yesBool = !yesBool;
    if (yesBool)
    {
        [inButton setImage:[UIImage imageNamed:@"xzPress"] forState:UIControlStateNormal];
    }else
        [inButton setImage:[UIImage imageNamed:@"xzPre"] forState:UIControlStateNormal];
    [self mButon];
}

-(void)xAction
{
    if (![SendService validateMobile:account.text]) {
        [SendService alerViewMessage:@"请输入正确的手机号码！"];
    }else
    {
        
        [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
        
        [self gotoAction];
        
        return;
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"我们将发送验证码短信至：%@",account.text]  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [alert show];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self gotoAction];

    }
}

-(void)gotoAction
{
    //    {
    //    model: {
    //    Username: 'xie6', //用户名
    //    Password: '123', //密码
    //    ConfirmPassword: 'xie10086', //确认密码
    //    Phone: '15000000000', //电话
    //    Email: '854642603@qq.com', //邮箱
    //    Referee: 'admin@hewulu.com' //推荐人
    //    } }
    
    NSMutableDictionary *postDic2 = [[NSMutableDictionary alloc] init];
    [postDic2 setObject:@"" forKey:@"Username"];
    [postDic2 setObject:@"" forKey:@"Password"];
    [postDic2 setObject:@"" forKey:@"ConfirmPassword"];
    [postDic2 setObject:account.text forKey:@"Phone"];
    [postDic2 setObject:@"" forKey:@"Email"];
    if (password.text.length != 0) {
        [postDic2 setObject:password.text forKey:@"Referee"];
    }else
        [postDic2 setObject:@"admin@hewulu.com" forKey:@"Referee"];
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    [postDic1 setObject:postDic2 forKey:@"model"];
    
    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray = [SendService CustomerRegister:postDic1];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSLog(@"myArray %@",myArray);
            [loading closeLoading];
            BOOL S = YES;
            for (NSDictionary* dic  in [myArray objectForKey:@"Entity"])
            {
                if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"key"]] isEqualToString:@"model.Phone"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@",[dic objectForKey:@"value"]]  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
                    [alert show];
                    S = NO;
                }
            }
            if (S)
            {
                ZCAndViewController *browser = [[ZCAndViewController alloc] init];
                browser.countString = account.text;
                if (password.text.length != 0)
                    browser.pString = password.text;
                browser.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:browser animated:YES];
            }
            
        });
    });
    
    //        yzmViewController *browser = [[yzmViewController alloc] init];
    //        browser.hidesBottomBarWhenPushed = YES;
    //        browser.numString = account.text;
    //        [self.navigationController pushViewController:browser animated:YES];
}

#pragma mark -
#pragma mark UITextFieldDelegate
//限制输入长度 by zwb 2011.9.6
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == account)
    {
        NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (text.length!=0&&yesBool)
        {
            [xButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            xButton.enabled = YES;
            [xButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
        }
        else
        {
            [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
            xButton.enabled = NO;
            [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
        }
        
    }
    return YES;
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == account) {
        [password becomeFirstResponder];
    }
    else
    {
        [textField resignFirstResponder];
    }
    [self mButon];

    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
